package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.BarcodeListAdapter;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.BarcodeEntity;
import com.eboss.sbs.model.BarcodeListEntity;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BarcodesActivity extends BaseActivity implements OnItemClickListener {

	private PullToRefreshScrollView scroolView;
	
	private MyListView listview;
	private BarcodeListAdapter adapter;
	private List<BarcodeEntity> datas;
	
	private EditText tv_search_key;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_barcodes);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "二维码管理 ");
		
		tv_search_key = (EditText)findViewById(R.id.tv_search_key);
		tv_search_key.setHint("输入名称查询");
		
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setMode(Mode.PULL_DOWN_TO_REFRESH);
		scroolView.setOnRefreshListener(new PullToRefreshViewEvent()); //下拉刷新 上拉加载
		
		datas = new ArrayList<BarcodeEntity>();
		listview = (MyListView)findViewById(R.id.listview);
		listview.setOnItemClickListener(this);
		adapter = new BarcodeListAdapter(this, datas);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.img_search).setOnClickListener(this);
		
		listview.setAdapter(adapter);
		
		getdata();
		
	}
	
	//下拉刷新 上拉加载
	public class PullToRefreshViewEvent implements OnRefreshListener<ScrollView>{
		@Override
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			getdata();
		}
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		String key = tv_search_key.getText().toString();
		if(!StringUtil.isNull(key)){
			params.put("key", key);
		}
		API.http_post(this, API.BARCODE_LIST, params, new HttpCallback<BarcodeListEntity>(this) {
			@Override
			public void onSuccess(int statusCode, BarcodeListEntity data) {
				scroolView.onRefreshComplete();
				if(data!=null){
					datas.clear();
					List<BarcodeEntity> datas_tmp = data.getBarcodes();
					if(datas_tmp!=null&datas_tmp.size()>0){
						datas.addAll(datas_tmp);
					}
					adapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onFailure(int statusCode, String message) {
				scroolView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<BarcodeListEntity>() {};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.img_search:
				getdata();
				break;
			default:break;
		}
		super.onClick(v);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		intent.setClass(mActivity, BarcodeMonthActivity.class);
		intent.putExtra(BarcodeMonthActivity.BARCODE_ID, datas.get(position).getBarcodeId());
		startActivity(intent);
	}
	
}
